package com.typing.controller;

import com.typing.entity.UserEntity;
import com.typing.entity.WordsEntity;
import com.typing.service.UserService;
import com.typing.service.WordsService;
import com.typing.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/words")
public class WordsController {

	@Resource
	WordsService wordService;

	@Resource
	UserService userService;

	private static String TEACHER = "教师";

	private static String STUDENT = "学生";

	private static String USER = "user";

	/**
	 * 查询全部单词
	 *
	 * @return
	 */
	@GetMapping("/getWords")
	public Result getWords() {
		List<WordsEntity> words = wordService.selectWords();
		return new Result().ok(words);
	}

	/**
	 * 设置分数
	 *
	 * @param user
	 * @return
	 */
	@PostMapping("/setScore")
	public Result setScore(@RequestBody UserEntity user) {
		if (user == null) {
			return new Result().systemError();
		}
		//	设置分数
		userService.setScore(user);
		return new Result().ok("分数设置成功");
	}

	/**
	 * 查询排名
	 *
	 * @return
	 */
	@GetMapping("/selectRanking/{id}")
	public Result selectRanking(@PathVariable("id") Integer id) {
		List<UserEntity> userEntities = null;

		//	查询当前用户是学生还是教师
		UserEntity user = userService.selectUserById(id);

		if (user == null) {
			return new Result().systemError();
		}

		//	根据登录信息获取不同排名
		if (TEACHER.equals(user.getRole())) {
			userEntities = userService.selectRankingTeacher();
		} else if (STUDENT.equals(user.getRole())) {
			userEntities = userService.selectRankingStudent();
		}

		return new Result().ok(userEntities);
	}

}
